package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.DictionaryBean;
import com.attendance.www.hr.beans.DictionaryPageBean;
import com.attendance.www.hr.dao.mapper.DictionaryExtMapper;
import com.attendance.www.hr.dao.mapper.DictionaryMapper;
import com.attendance.www.hr.dao.mapper.RequestAuthorityMapper;
import com.attendance.www.hr.entitys.Dictionary;
import com.attendance.www.hr.entitys.DictionaryExample;
import com.attendance.www.hr.entitys.DictionaryKey;
import com.attendance.www.hr.entitys.RequestAuthority;
import com.attendance.www.hr.entitys.RequestAuthorityExample;
import com.attendance.www.hr.service.IDictionaryService;

@Service
public class DictionaryServiceImpl implements IDictionaryService {
	@Resource
	private DictionaryMapper dictionaryDao;
	@Resource
	private DictionaryExtMapper dictionaryExtDao;
	@Resource
	private RequestAuthorityMapper requestAuthorityDao; 

	@Override
	public DictionaryPageBean getDictionaryList(Integer startNum, Integer pageCount) {

		List<Dictionary> dictionaryList = dictionaryExtDao.getDictionaryList(startNum, pageCount);
		int pageTotal = dictionaryExtDao.getDictionaryListPageTotal();
		DictionaryPageBean dictionaryPageBean = new DictionaryPageBean();
		dictionaryPageBean.setDictionaryList(dictionaryList);
		dictionaryPageBean.setPageTotal(pageTotal);

		return dictionaryPageBean;
	}

	@Override
	public List<DictionaryBean> getDictionaryListByDicCls(String dicCls,String dicCd,Integer employeeId) {
		DictionaryExample example = new DictionaryExample();
		DictionaryExample.Criteria criteria = example.createCriteria();
		criteria.andDicClsEqualTo(dicCls);
		if(StringUtils.isNotBlank(dicCd)){
			criteria.andDicCdGreaterThan(dicCd);
			criteria.andDicCdNotEqualTo("99");
		}
		criteria.andStatusEqualTo("1");
		example.setOrderByClause("dic_cd+'' asc");
		List<Dictionary> dictionaryList = dictionaryDao.selectByExample(example);
		List<DictionaryBean> dictionaryBeanList = new ArrayList<DictionaryBean>();
		
		RequestAuthorityExample requestAuthorityExample=new RequestAuthorityExample();
		if (employeeId!=null) {
			requestAuthorityExample.createCriteria().andEmployeeIdEqualTo(employeeId);
		}
		List<RequestAuthority> requestAuthorityList=requestAuthorityDao.selectByExample(requestAuthorityExample);
		if (dictionaryList != null) {
			for (int i = 0; i < dictionaryList.size(); i++) {
				DictionaryBean dictionaryBean = new DictionaryBean();
				dictionaryBean.setDicCd(dictionaryList.get(i).getDicCd());
				dictionaryBean.setDicCls(dictionaryList.get(i).getDicCls());
				dictionaryBean.setDicClsName(dictionaryList.get(i).getDicClsName());
				dictionaryBean.setDicName(dictionaryList.get(i).getDicName());
				//判断休假类型
				if (dicCls.equals("5")) {
					if(!dictionaryBean.getDicCd().equals("10")){
						//判断是否有产假权限
						if(dictionaryBean.getDicCd().equals("13")){
							if(requestAuthorityList.size()>0){
								boolean type=false;
								//循环申请权限
								for(int j=0;j<requestAuthorityList.size();j++){
									RequestAuthority ra=requestAuthorityList.get(j);
									if(ra!=null){
										if(ra.getRequestType()==Integer.parseInt(dictionaryBean.getDicCd())){
											type=true;
											break;
										}
									}
								}
								if (type) {
									dictionaryBeanList.add(dictionaryBean);
								}
							}
						}else{
							dictionaryBeanList.add(dictionaryBean);
						}
					}
				}else{
					dictionaryBeanList.add(dictionaryBean);
				}
			}
		}
		return dictionaryBeanList;
	}
	@Override
	public List<DictionaryBean> getDictionaryByDicCls(String dicCls) {
		DictionaryExample example=new DictionaryExample();
		DictionaryExample.Criteria criteria = example.createCriteria();
		criteria.andDicClsEqualTo(dicCls);
		List<Dictionary> dictionaryList=dictionaryDao.selectByExample(example);
		List<DictionaryBean> dictionaryBeanList=new ArrayList<DictionaryBean>();
		if(dictionaryList!=null){
			for(int i=0;i<dictionaryList.size();i++){
				DictionaryBean dictionaryBean=new DictionaryBean();
				dictionaryBean.setDicCls(dictionaryList.get(i).getDicCls());
				dictionaryBean.setDicClsName(dictionaryList.get(i).getDicClsName());
				dictionaryBean.setDicCd(dictionaryList.get(i).getDicCd());
				dictionaryBean.setDicName(dictionaryList.get(i).getDicName());
				dictionaryBeanList.add(dictionaryBean);
			}
		}
		return dictionaryBeanList;
		
	}
	@Override
	public Dictionary getDictionaryListByDicClsORDicCd(String dicCls, String dicCd) {
		DictionaryKey key = new DictionaryKey();
		key.setDicCd(dicCd);
		key.setDicCls(dicCls);
		return dictionaryDao.selectByPrimaryKey(key);
	}

	@Override
	public boolean saveDictionary(Dictionary dictionary) {
		int record = dictionaryDao.insert(dictionary);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean updateDictionary(Dictionary dictionary) {
		int record = dictionaryDao.updateByPrimaryKey(dictionary);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean deleteDictionary(Dictionary dictionary) {
		int record = dictionaryDao.deleteByPrimaryKey(dictionary);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

}
